# -*- tcl -*-

set send_slow {1 0.001}
set timeout 30

proc delay {} {
    sleep 0.5
}

proc chat {outstr instr {keep false}} {
    send -s $outstr
    delay
    if $keep {
        expect {
            timeout {puts "timeout"; exit 1}
            -notransfer $instr
        }
    } else {
        expect {
            timeout {puts "timeout"; exit 1}
            $instr
        }
    }
}

proc rechat {outstr inre {keep false}} {
    send -s $outstr
    delay
    if $keep {
        expect {
            timeout {puts "timeout"; exit 1}
            -notransfer -re $inre
        }
    } else {
        expect {
            timeout {puts "timeout"; exit 1}
            -re $inre
        }
    }
}

proc next {} {
    delay
    expect {
        timeout {puts "timeout"; exit 1}
        {bash-*[$#] $}
    }
}

proc cancel {} {
    send -s "\r"
    next
}

proc brexit {} {
    send -s "exit\r"
    expect {
        timeout {puts "timeout"; exit 1}
        eof     {exit 0}
    }
}


# Set up and ensure we have an empty environment as on some systems
# (debian) PS1 leaks into the newly spawned shell which causes our
# tests to fail.
spawn env -u PS1 bash --norc -i
next
send ". /usr/share/bash-completion/bash_completion; . ../../../data/completion/snap\n"
next
